package com.yunxin.core.infra.persistence.repository.channel;

import com.yunxin.core.domain.model.channel.ChannelPromotionUnitPriceConfig;
import com.yunxin.core.domain.model.channel.ChannelPromotionUnitPriceConfigRepository;
import com.yunxin.core.infra.persistence.converter.ChannelConvertor;
import com.yunxin.core.infra.persistence.entity.channel.ChannelPromotionUnitPriceConfigDO;
import com.yunxin.core.infra.persistence.mapper.channel.ChannelPromotionUnitPriceConfigExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

/**
 * @author zero°
 * @since 2023-07-12
 */
@Repository
@RequiredArgsConstructor
public class ChannelPromotionUnitPriceConfigRepositoryImpl implements ChannelPromotionUnitPriceConfigRepository {

    private final ChannelPromotionUnitPriceConfigExtendMapper channelPromotionUnitPriceConfigExtendMapper;

    private final ChannelConvertor channelConvertor;
    @Override
    public boolean saveChannelPromotionUnitPriceConfig(ChannelPromotionUnitPriceConfig channelPromotionUnitPriceConfig) {
        ChannelPromotionUnitPriceConfigDO channelPromotionUnitPriceConfigDO = channelConvertor.formChannelPromotionUnitPriceConfig(channelPromotionUnitPriceConfig);
        int count = channelPromotionUnitPriceConfigExtendMapper.insertSelective(channelPromotionUnitPriceConfigDO);
        return count > 0;
    }

    @Override
    public boolean updateChannelPromotionUnitPriceConfig(ChannelPromotionUnitPriceConfig channelPromotionUnitPriceConfig) {
        ChannelPromotionUnitPriceConfigDO channelPromotionUnitPriceConfigDO = channelConvertor.formChannelPromotionUnitPriceConfig(channelPromotionUnitPriceConfig);
        int count = channelPromotionUnitPriceConfigExtendMapper.updateByPrimaryKeySelective(channelPromotionUnitPriceConfigDO);
        return count > 0;
    }

    @Override
    public ChannelPromotionUnitPriceConfig getByPromotionId(Long promotionId) {
        ChannelPromotionUnitPriceConfigDO channelPromotionUnitPriceConfigDO = channelPromotionUnitPriceConfigExtendMapper.selectByPromotionId(promotionId);
        return channelConvertor.toChannelPromotionUnitPriceConfig(channelPromotionUnitPriceConfigDO);
    }
}
